package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.ProcessWorkstation;
import com.mesSystem.entity.ProductDefinition;
import com.mesSystem.entity.ProductProcessBom;
import com.mesSystem.entity.Workstation;
import com.mesSystem.service.impl.ProcessRouteService;
import com.mesSystem.service.impl.ProcessService;
import com.mesSystem.service.impl.ProcessWorkstationService;
import com.mesSystem.service.impl.ProductProcessBomService;
import com.sun.org.apache.xalan.internal.xslt.Process;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("productProcessBom")
public class ProductProcessBomController {

    @Autowired
    ProductProcessBomService productProcessBomService;
    @Autowired
    ProcessRouteService processRouteService;
    @Autowired
    ProcessWorkstationService processWorkstationService;

    /**
     * 新增
     *
     * @param productProcessBom
     * @return
     */
    @PostMapping("increase")
    public Result increase(@RequestBody ProductProcessBom productProcessBom) {

        productProcessBomService.save(productProcessBom);
        return Result.ok("创建成功");
    }

    /**
     * 根据id删除
     *
     * @param productProcessBomId
     * @return
     */
    @DeleteMapping("delete")
    public Result delete(Long productProcessBomId) {
        productProcessBomService.removeById(productProcessBomId);
        return Result.ok("删除成功");
    }


    /**
     * 根据产品id、工序id、工位类型id，查询某工位类型下的所有物料bom信息
     *
     * @param productDefinitionId
     * @param processId
     * @param workstationTypeId
     * @return
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "productDefinitionId") Long productDefinitionId,
                        @RequestParam(value = "processId") Long processId,
                        @RequestParam(value = "workstationTypeId") Long workstationTypeId) {
        log.info("{}--{}--{}", productDefinitionId, processId, workstationTypeId);
        QueryWrapper<ProductProcessBom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_definition_id", productDefinitionId);
        queryWrapper.eq("process_id", processId);
        queryWrapper.in("workstation_type_id", workstationTypeId);
        List<ProductProcessBom> list = productProcessBomService.list(queryWrapper);
        log.info(list.toString());
        return Result.ok(list);
    }

    public Result queryByBomId(@RequestParam(value = "page", defaultValue = "1") Integer page,
                               @RequestParam(value = "size", defaultValue = "10") Integer size,
                               @RequestParam(value = "productProcessBomId") Long productProcessBomId) {

        // 根据bomId获取工艺信息，根据工艺信息获取工序集合
        return Result.ok();
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody ProductProcessBom productProcessBom) {
        productProcessBomService.updateById(productProcessBom);
        log.info("update:{}", productProcessBom);
        return Result.ok("更新成功");
    }
}
